<?php

/**
 * 出发地识别
 */
class FromDestParseBehavior extends Behavior {
    // 行为参数定义
    protected $options = array(
        'AutoParseFromDest' => true,//默认开启自动识别出发地
    );

    public function run(&$params) {
        if(!C("AutoParseFromDest")) return;
        
        $now_from_dest = array();

        //尝试解析COOKIE中设置的当前出发地
        $cookie_dest = cookie('from_dest');
        if(!empty($cookie_dest)) {
            $now_from_dest = M("classsys")
                            ->field('classsys0 as id,classsys1 as name')
                            ->where("classsys3=542 and classsys0={$cookie_dest}")
                            ->order("classsys4 desc")
                            ->find();
            trace('通过cookie取得出发地',dump($now_from_dest,false));
        }
        //TODO 尝试获取用户常用出发地列表设置
        else {
            /*
            $uid = $_COOKIE["uchome_uid"];
            $uchome_auth = $_COOKIE["uchome_auth"];
            $choosechufa = $cmr->table(UHDQ."userchuafadi U")
                               ->join(DQ."classsys F on U.chuafaid = F.classsys0")
                               ->where("U.uid=".$uid)
                               ->select();
            if( strlen($uchome_auth)>5 && !empty($choosechufa))
                $now_from_dest = $choosechufa[0];
            */
        }
        //以上都没有获取到出发地信息，则通过IP识别
        if(empty($now_from_dest)) {
            //$ip_add = get_client_ip();
            //$ip_add = "220.248.3.202";//debug上海
            //$ip_add = "222.28.239.52";//debug北京
            $ip_add = "124.226.251.207";//debug广西
            $trace_info['客户端IP地址'] = $ip_add;//debug

            $ip_exp = explode('.', $ip_add);
            $ip_code = 0;
            for($i=0; $i<count($ip_exp); $i++)
                $ip_code += $ip_exp[$i] * pow( 256, 3-$i );
            $Addr = M("ipaddress")->field("Province,City")->where("$ip_code between IP1 and IP2")->find();
            //trace("sql",M("ipaddress")->getLastSQL());//debug
            $trace_info['解析IP地址位置'] = dump($Addr,false);//debug

            //查找IP归属出发地
            $dest_id = M("info")->field("info2")->where("info3 like '%{$Addr['Province']}%'")->find();
            $dest_id = $dest_id["info2"];
            //如果没有匹配项则查找默认出发地
            if(empty($dest_id)) {
                $dest_id = M("info")->field("info2")->where("info8=1212")->find();
                $dest_id = $dest_id["info2"];
                $trace_info['尝试解析默认出发地'] = $dest_id;//debug
            } else {
                $trace_info['匹配到指定出发地'] = $dest_id;//debug
            }

            if($dest_id) {
                $dest_info = M("classsys")->field('classsys0,classsys1')->where("classsys0=$dest_id")->find();
                $now_from_dest["id"] = $dest_info['classsys0'];
                $now_from_dest["name"] = $dest_info['classsys1'];
                $trace_info['出发地解析成功'] = dump($now_from_dest,false);//debug
            } else {
                $trace_info['没有设置默认出发地'] = "出发地解析彻底失败！";//debug
            }
            trace($trace_info);
        }

        //定义全站常量：当前出发地
        define('__FromDestID__', $now_from_dest['id']);
        define('__FromDestName__', $now_from_dest['name']);
    }
}